package com.wowangz.cms.syscore.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wowangz.cms.common.pojo.AjaxDataEntity;
import com.wowangz.cms.component.datagrid.pojo.DataResult;

import net.sf.json.JSONObject;


/**
 * Ajax提交后的返回实体
 * @author leiyang3
 *
 */
public class JSONFactory {
	
	final protected static Logger logger = LoggerFactory.getLogger(JSONFactory.class);
	
	/**
	 * 用于表单AJAX请求信息后转换为json
	 * @param entity
	 * @param userContext
	 * @return
	 */
	public static String toJSONString(AjaxDataEntity entity, UserContext userContext) {
		
		String json = "";
		entity.setMessagetype(userContext.getMessageType());
		entity.setMessage(userContext.getMessage());
		json = JSONObject.fromObject(entity).toString();
		logger.info("AJAX请求返回信息：" + json);
		return json;
		
	}
	
	/**
	 * 用于表单AJAX请求信息后转换为json
	 * 用于ServiceController
	 * @param serviceResponse
	 * @param serviceContext
	 * @return
	 */
	public static String toJSONString(ServiceResponse serviceResponse, ServiceContext serviceContext) {
		
		String json = "";
		serviceResponse.setMessagetype(serviceContext.getMessageType());
		serviceResponse.setMessage(serviceContext.getMessage());
		json = JSONObject.fromObject(serviceResponse).toString();
		logger.info("服务请求返回信息：" + json);
		return json;
		
	}
	
	/**
	 * 用于分页查询请求信息后转换为json
	 * @param dataResult
	 * @param userContext
	 * @return
	 */
	public static String toJSONString(DataResult entity, UserContext userContext) {
		
		String json = "";
		entity.setMessagetype(userContext.getMessageType());
		entity.setMessage(userContext.getMessage());
		json = JSONObject.fromObject(entity).toString();
		//logger.info("AJAX请求返回信息：" + json);
		return json;
		
	}
	
	
	public static void main(String args[]){
		
		UserContext userContext = new UserContext();
		//userContext.USERID = 1;
		
		AjaxDataEntity entity = new AjaxDataEntity();
		entity.addData("id", "111");
		JSONFactory.toJSONString(entity, userContext);
	}
}
